﻿namespace SLClient.CustomerControls
{
    using SLClient.Extension;
    using SLClient.Infrastructure;
    using SLClient.ServiceAPI;
    using SLClient.ViewModels;
    using System;
    using System.Collections.ObjectModel;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Input;

    public class ConfirmWindowViewModel : ViewModelBase
    {
        private BetControl _betControl;
        private BetControlNx _betControlNx;
        private string _betName;
        private Visibility _completeVisibity =Visibility.Collapsed;
        private Visibility _confirmVisibity = Visibility.Visible;
        private readonly ConfirmWindow _confirmWindow;
        private string _message;
        private Visibility _processVisibity = Visibility.Collapsed;
        private SLClient.ServiceAPI.UserPlayInfo _userPlayInfo;

        public ConfirmWindowViewModel(BetControl betControl, BetControlNx betControlNx, ConfirmWindow confirmWindow, SLClient.ServiceAPI.UserPlayInfo userPlayInfo, string betName)
        {
            this._confirmWindow = confirmWindow;
            this._betName = betName;
            this._betControl = betControl;
            this._betControlNx = betControlNx;
            this._userPlayInfo = userPlayInfo;
            this.CommandInit();
        }

        public void Close()
        {
            this._confirmWindow.Close();
        }

        public void CommandInit()
        {
            RelayCommand command = new RelayCommand(new Action(this.Save)) {
                IsEnabled = true
            };
            this.SaveCommand = command;
        }

        public void Save()
        {
            EventHandler<CreateUserPlayInfoCompletedEventArgs> handler = null;
            EventHandler<CreateNegativUserPlayInfosCompletedEventArgs> handler2 = null;
            decimal activityMoney;
            decimal? danBeiMoney;
            decimal? jiangJin;
            this.ProcessVisibity =Visibility.Visible;
            this.ConfirmVisibity = Visibility.Collapsed;
            ServiceClient serviceClient = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            serviceClient.GetUserByUserIdCompleted += delegate (object obj, GetUserByUserIdCompletedEventArgs arg) {
                ViewDispatcher.LoginUser.ActivityMoney = arg.Result.ActivityMoney;
                ViewDispatcher.LoginUser.FreezeMoney = arg.Result.FreezeMoney;
                ViewDispatcher.LoginUser.TotleMoney = arg.Result.TotleMoney;
            };
            if (this._userPlayInfo.Orization)
            {
                activityMoney = ViewDispatcher.LoginUser.ActivityMoney;
                int? nullable = this._userPlayInfo.BetAmount;
                danBeiMoney = this._userPlayInfo.DanBeiMoney;
                if (((activityMoney - ((nullable.HasValue & danBeiMoney.HasValue) ? new decimal?(nullable.GetValueOrDefault() * danBeiMoney.GetValueOrDefault()) : ((decimal?) (jiangJin = null)))) + ((this._userPlayInfo.BetAmount * this._userPlayInfo.DanBeiMoney) * this._userPlayInfo.Fiandian)) < 0M)
                {
                    this.Message = "信用余额不足";
                    this.ProcessVisibity = Visibility.Collapsed;
                    this.ConfirmVisibity = Visibility.Collapsed;
                    this.CompleteVisibity = Visibility.Visible;
                }
                else
                {
                    if (handler == null)
                    {
                        handler = delegate (object obj, CreateUserPlayInfoCompletedEventArgs arg) {
                            this.ProcessVisibity = Visibility.Collapsed;
                            if (arg.Result)
                            {
                                this.Message = "投注成功";
                                serviceClient.GetUserByUserIdAsync(ViewDispatcher.LoginUser.UserId);
                                MainpageViewModel.NewOrders();
                                this._betControl.CurrentBetType = BetType.BetSuccess;
                            }
                            else
                            {
                                this.Message = "投注失败";
                            }
                            this.CompleteVisibity = Visibility.Visible;
                        };
                    }
                    serviceClient.CreateUserPlayInfoCompleted += handler;
                    serviceClient.CreateUserPlayInfoAsync(this._userPlayInfo);
                }
            }
            else
            {
                decimal? nullable4;
                activityMoney = ViewDispatcher.LoginUser.ActivityMoney;
                danBeiMoney = this._userPlayInfo.BetAmount * this._userPlayInfo.DanBeiMoney;
                jiangJin = this._userPlayInfo.JiangJin;
                jiangJin = jiangJin.HasValue ? new decimal?(((decimal?)jiangJin).GetValueOrDefault()) : ((decimal?) (nullable4 = null));
                if (((activityMoney - ((danBeiMoney.HasValue & jiangJin.HasValue) ? new decimal?(danBeiMoney.GetValueOrDefault() * jiangJin.GetValueOrDefault()) : ((decimal?) (nullable4 = null)))) + ((this._userPlayInfo.BetAmount * this._userPlayInfo.DanBeiMoney) * this._userPlayInfo.Fiandian)) < 0M)
                {
                    this.Message = "信用余额不足";
                    this.ProcessVisibity = Visibility.Collapsed;
                    this.ConfirmVisibity = Visibility.Collapsed;
                    this.CompleteVisibity = Visibility.Visible;
                }
                else
                {
                    if (handler2 == null)
                    {
                        handler2 = delegate (object obj, CreateNegativUserPlayInfosCompletedEventArgs arg) {
                            this.ProcessVisibity = Visibility.Collapsed;
                            int result = arg.Result;
                            if (result == this._userPlayInfo.BetAmount)
                            {
                                this.Message = "投注成功";
                            }
                            result = arg.Result;
                            if (result != this._userPlayInfo.BetAmount)
                            {
                                int? betAmount = this._userPlayInfo.BetAmount;
                                result = arg.Result;
                                this.Message = string.Format("投注成功{0}条,失败{1}条", arg.Result, betAmount.HasValue ? new int?(betAmount.GetValueOrDefault() - result) : null);
                            }
                            serviceClient.GetUserByUserIdAsync(ViewDispatcher.LoginUser.UserId);
                            MainpageViewModel.NewOrders();
                            this.CompleteVisibity = Visibility.Visible;
                            this._betControlNx.CurrentBetType = BetType.BetSuccess;
                        };
                    }
                    serviceClient.CreateNegativUserPlayInfosCompleted += handler2;
                    string[] strArray = this._userPlayInfo.ContentNumber.Split(new char[] { '|' });
                    ObservableCollection<SLClient.ServiceAPI.UserPlayInfo> userPlayInfos = new ObservableCollection<SLClient.ServiceAPI.UserPlayInfo>();
                    foreach (string str in strArray)
                    {
                        SLClient.ServiceAPI.UserPlayInfo info2 = new SLClient.ServiceAPI.UserPlayInfo {
                            BetAmount = 1,
                            ContentNumber = str,
                            DanBeiMoney = this._userPlayInfo.DanBeiMoney,
                            Fiandian = this._userPlayInfo.Fiandian,
                            JiangJin = this._userPlayInfo.JiangJin,
                            LotteryCatagoryId = this._userPlayInfo.LotteryCatagoryId,
                            Orization = false,
                            PlayTypeId = this._userPlayInfo.PlayTypeId,
                            PlayTypeInfoId = this._userPlayInfo.PlayTypeInfoId,
                            QIHao = this._userPlayInfo.QIHao,
                            UserId = this._userPlayInfo.UserId
                        };
                        SLClient.ServiceAPI.UserPlayInfo item = info2;
                        userPlayInfos.Add(item);
                    }
                    serviceClient.CreateNegativUserPlayInfosAsync(userPlayInfos);
                }
            }
        }

        public string BetAmount
        {
            get
            {
                return (this.UserPlayInfo.BetAmount + "注");
            }
        }

        public string BetName
        {
            get
            {
                return this._betName;
            }
            set
            {
                this._betName = value;
                base.Notify<string>(() => this.BetName);
                //base.Notify<string>(System.Linq.Expressions.Expression.Lambda<Func<string>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(ConfirmWindowViewModel)), (MethodInfo)(ConfirmWindowViewModel.get_BetName)), new ParameterExpression[0]));
            }
        }

        public string BetOrient
        {
            get
            {
                return (this._userPlayInfo.Orization ? "正向" : "逆向");
            }
        }

        public string BetTotle
        {
            get
            {
                return string.Format("{0:F2}元", this._userPlayInfo.BetAmount * this._userPlayInfo.DanBeiMoney);
            }
        }

        public Visibility CompleteVisibity
        {
            get
            {
                return this._completeVisibity;
            }
            set
            {
                this._completeVisibity = value;
                base.Notify<Visibility>(() => this.CompleteVisibity);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(ConfirmWindowViewModel)), (MethodInfo)(ConfirmWindowViewModel.get_CompleteVisibity)), new ParameterExpression[0]));
            }
        }

        public Visibility ConfirmVisibity
        {
            get
            {
                return this._confirmVisibity;
            }
            set
            {
                this._confirmVisibity = value;
                base.Notify<Visibility>(() => this.ConfirmVisibity);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(ConfirmWindowViewModel)), (MethodInfo)(ConfirmWindowViewModel.get_ConfirmVisibity)), new ParameterExpression[0]));
            }
        }

        public string DanBeiMoney
        {
            get
            {
                return string.Format("{0:F2}元", this.UserPlayInfo.DanBeiMoney);
            }
        }

        public string Message
        {
            get
            {
                return this._message;
            }
            set
            {
                this._message = value;
                base.Notify<string>(() => this.Message);
                //base.Notify<string>(System.Linq.Expressions.Expression.Lambda<Func<string>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(ConfirmWindowViewModel)), (MethodInfo)(ConfirmWindowViewModel.get_Message)), new ParameterExpression[0]));
            }
        }

        public Visibility ProcessVisibity
        {
            get
            {
                return this._processVisibity;
            }
            set
            {
                this._processVisibity = value;
                base.Notify<Visibility>(() => this.ProcessVisibity);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(ConfirmWindowViewModel)), (MethodInfo)(ConfirmWindowViewModel.get_ProcessVisibity)), new ParameterExpression[0]));
            }
        }

        public ICommand SaveCommand { get; set; }

        public SLClient.ServiceAPI.UserPlayInfo UserPlayInfo
        {
            get
            {
                return this._userPlayInfo;
            }
            set
            {
                this._userPlayInfo = value;
                base.Notify<SLClient.ServiceAPI.UserPlayInfo>(() => this.UserPlayInfo);
                //base.Notify<SLClient.ServiceAPI.UserPlayInfo>(System.Linq.Expressions.Expression.Lambda<Func<SLClient.ServiceAPI.UserPlayInfo>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(ConfirmWindowViewModel)), (MethodInfo)(ConfirmWindowViewModel.get_UserPlayInfo)), new ParameterExpression[0]));
            }
        }
    }
}

